package com.aididi.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: zx.xu
 * @Description:
 */
public class ExecutorsDemo {

    public static void main(String[] args) {
        //创建使用单个线程的线程池
        ExecutorService es1 = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            es1.submit(() -> System.out.println("SingleThread:--"+Thread.currentThread().getName() + "正在执行任务"));
        }
        //创建使用固定线程数的线程池
        ExecutorService es2 = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 10; i++) {
            es2.submit(() -> System.out.println("FixedThread:--"+Thread.currentThread().getName() + "正在执行任务"));
        }
        //创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程.
        ExecutorService es3 = Executors.newCachedThreadPool();
        for (int i = 0; i < 20; i++) {
            es3.submit(() -> System.out.println("CachedThread:--"+Thread.currentThread().getName() + "正在执行任务"));
        }
        //创建拥有固定线程数量的定时线程任务的线程池
        ScheduledExecutorService es4 = Executors.newScheduledThreadPool(2);
        System.out.println("时间：" + System.currentTimeMillis());
        for (int i = 0; i < 5; i++) {
            es4.schedule(() -> System.out.println("ScheduledThread:--"+"时间："+System.currentTimeMillis()+"--"+Thread.currentThread().getName() + "正在执行任务"),3, TimeUnit.SECONDS);
        }
        //创建只有一个线程的定时线程任务的线程池
        ScheduledExecutorService es5 = Executors.newSingleThreadScheduledExecutor();
        System.out.println("时间：" + System.currentTimeMillis());
        for (int i = 0; i < 5; i++) {
            es5.schedule(() -> System.out.println("SingleThreadScheduled:--"+"时间："+System.currentTimeMillis()+"--"+Thread.currentThread().getName() + "正在执行任务"),3, TimeUnit.SECONDS);
        }
    }

}
